﻿Imports Tools4Fools.Interop.VBAExtensibility

Public Class fMain


    ' Private Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    'Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

    Private Sub fMain_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        '  Me.Top = GetCBTop()
        '  Me.Left = GetCBLeft()
        '  MsgBox(GetCBLeft)
    End Sub

    'abd.hWnd = FindWindow("Shell_TrayWnd", "") 'Tasbar Handle
    ' ABD.hWnd = FindWindow("Shell_TrayWnd", "")

    Private Sub fMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        MsgBox(VBE.CodePanes.Count)
    End Sub

    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
        MsgBox(VBE.ActiveVBProject.VBComponents.Count)
    End Sub

    Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs)


    End Sub

    Private Sub ToolStripButton4_Click(sender As Object, e As EventArgs) Handles ToolStripButton4.Click
        mIndent.IndentProjectCode()
    End Sub

    Private Sub ToolStripButton5_Click(sender As Object, e As EventArgs) Handles ToolStripButton5.Click
        mIndent.IndentModuleCode()
    End Sub

    Private Sub ToolStripButton3_Click_1(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        Me.Hide()
    End Sub

    Private Sub ToolStripButton6_Click(sender As Object, e As EventArgs) Handles ToolStripButton6.Click
        MsgBox(FindWindow("XLDESK", ""))
        MsgBox(Me.Handle)
        MsgBox(FindWindow("Shell_TrayWnd", "")) 'Tasbar Handle

        SetParent(FindWindow("XLDESK", ""), Me.Handle)

        Dim hand As String
        hand = InputBox("whats the handel?")

        SetParent(hand, Me.Handle)

        '  Me.Show()
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Show()
        Me.TopLevel = True

    End Sub

    Private Sub NotifyIcon1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseClick
        Me.Show()
        Me.TopLevel = True
    End Sub

    Private Sub ToolStripButton7_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub ToolStripButton7_Click_1(sender As Object, e As EventArgs) Handles ToolStripButton7.Click
        MsgBox(TotalLinesInProject(VBE.ActiveVBProject))
    End Sub


#Region "Functioal Line counting code via Chip"

    Public Function TotalCodeLinesInVBComponent(VBComp As VBComponent) As Long
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' This returns the total number of code lines (excluding blank lines and
        ' comment lines) in the VBComponent referenced by VBComp. Returns -1
        ' if the VBProject is locked.
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Dim N As Long
        Dim S As String
        Dim LineCount As Long

        If VBComp.Collection.Parent.Protection = vbext_ProjectProtection.vbext_pp_locked Then
            TotalCodeLinesInVBComponent = -1
            Exit Function
        End If

        With VBComp.CodeModule
            For N = 1 To .CountOfLines
                S = .Lines(N, 1)
                If Trim(S) = vbNullString Then
                    ' blank line, skip it
                ElseIf Strings.Left(Trim(S), 1) = "'" Then
                    ' comment line, skip it
                Else
                    LineCount = LineCount + 1
                End If
            Next N
        End With
        TotalCodeLinesInVBComponent = LineCount
    End Function


    Public Function TotalLinesInProject(Optional VBProj As VBProject = Nothing) As Long
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' This returns the total number of lines in all components of the VBProject
        ' referenced by VBProj. If VBProj is missing, the VBProject of the ActiveWorkbook
        ' is used. Returns -1 if the VBProject is locked.
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Dim VBP As VBProject
        Dim VBComp As VBComponent
        Dim LineCount As Long

        If VBProj Is Nothing Then
            VBP = VBE.ActiveVBProject
        Else
            VBP = VBProj
        End If

        If VBP.Protection = vbext_ProjectProtection.vbext_pp_locked Then
            TotalLinesInProject = -1
            Exit Function
        End If

        For Each VBComp In VBP.VBComponents
            LineCount = LineCount + VBComp.CodeModule.CountOfLines
        Next VBComp

        TotalLinesInProject = LineCount
    End Function


    'Public Function TotalCodeLinesInVBComponent(VBComp As VBComponent) As Long
    '    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '    ' This returns the total number of code lines (excluding blank lines and
    '    ' comment lines) in the VBComponent referenced by VBComp. Returns -1
    '    ' if the VBProject is locked.
    '    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '    Dim N As Long
    '    Dim S As String
    '    Dim LineCount As Long

    '    If VBComp.Collection.Parent.Protection = vbext_ProjectProtection.vbext_pp_locked Then
    '        TotalCodeLinesInVBComponent = -1
    '        Exit Function
    '    End If

    '    With VBComp.CodeModule
    '        For N = 1 To .CountOfLines
    '            S = .Lines(N, 1)
    '            If Trim(S) = vbNullString Then
    '                ' blank line, skip it
    '            ElseIf Strings.Left(Trim(S), 1) = "'" Then
    '                ' comment line, skip it
    '            Else
    '                LineCount = LineCount + 1
    '            End If
    '        Next N
    '    End With
    '    TotalCodeLinesInVBComponent = LineCount
    'End Function



    Public Function TotalCodeLinesInProject(VBProj As VBProject) As Long
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' This returns the total number of code lines (excluding blank lines and
        ' comment lines) in all VBComponents of VBProj. Returns -1 if VBProj
        ' is locked.
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        Dim VBComp As VBComponent
        Dim LineCount As Long
        If VBProj.Protection = vbext_ProjectProtection.vbext_pp_locked Then
            TotalCodeLinesInProject = -1
            Exit Function
        End If
        For Each VBComp In VBProj.VBComponents
            LineCount = LineCount + TotalCodeLinesInVBComponent(VBComp)
        Next VBComp

        TotalCodeLinesInProject = LineCount
    End Function


#End Region

    Private Sub ToolStripButton8_Click(sender As Object, e As EventArgs) Handles ToolStripButton8.Click
        MsgBox(TotalCodeLinesInProject(VBE.ActiveVBProject))
    End Sub
End Class


